package com.wanlian.service.impl;

import com.wanlian.dao.RoleGroupAuthorityDao;
import com.wanlian.entity.*;
import com.wanlian.model.ResultModel;
import com.wanlian.service.*;
import com.wanlian.util.DateUtil;
import com.wanlian.vo.AuthorityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RoleGroupAuthorityServiceImpl implements RoleGroupAuthorityService {

    @Autowired
    private RoleGroupAuthorityDao groupAuthorityDao;


    @Override
    public ResultModel doAdd(AuthorityVo authorityVo) {
        ResultModel resultModel = new ResultModel(true);
        if (authorityVo.getRolegroupid() == 0) {
            resultModel.setBresult(false);
            resultModel.setMessage("请选择权限组");
            return resultModel;
        }
        //删除旧权限
        List<RoleGroupAuthority> list = groupAuthorityDao.queryByGid(authorityVo.getRolegroupid());
        if (list.size() > 0) {
            for (RoleGroupAuthority rga : list) {
                groupAuthorityDao.delete(rga.getId());
            }
        }
        //添加新权限
        if (authorityVo.getAuthorityids() != "" && authorityVo.getAuthorityids() != null) {
            String[] authorityIds = authorityVo.getAuthorityids().split(",");
            if (authorityIds.length > 0) {
                for (String authorityId : authorityIds) {
                    int aid = Integer.parseInt(authorityId);
                    RoleGroupAuthority roleGroupAuthority = new RoleGroupAuthority();
                    roleGroupAuthority.setCreateid(authorityVo.getUserId());
                    roleGroupAuthority.setIsenable(true);
                    roleGroupAuthority.setIsdel(false);
                    roleGroupAuthority.setLastupdatetime(DateUtil.currentTimestamp());
                    roleGroupAuthority.setGroupid(authorityVo.getRolegroupid());
                    roleGroupAuthority.setAuthorityid(aid);
                    groupAuthorityDao.add(roleGroupAuthority);
                }
            }
        }
        resultModel.setMessage("修改成功");
        return resultModel;

    }


    @Override
    public ResultModel doDel(RoleGroupAuthority roleGroupAuthority) {
        ResultModel resultModel = new ResultModel(true);
        RoleGroupAuthority db_roleGroupAuthority = groupAuthorityDao.queryByAuthorityId(roleGroupAuthority.getId());
        if (db_roleGroupAuthority == null) {
            resultModel.setBresult(false);
            resultModel.setMessage("该权限不存在，删除失败");
            return resultModel;
        } else {
            db_roleGroupAuthority.setIsenable(false);
            groupAuthorityDao.delete(roleGroupAuthority.getId());
            resultModel.setBresult(true);
            resultModel.setMessage("删除成功");
            return resultModel;
        }
    }

    @Override
    public ResultModel queryByParam(OrgRoleGroup orgRoleGroup) {
        return groupAuthorityDao.queryByParams(orgRoleGroup);
    }

    @Override
    public RoleGroupAuthority queryByGAid(int groupId, int authorityId) {
        return groupAuthorityDao.queryByGAid(groupId,authorityId);
    }
}
